package astudy.networking.ftp;

import java.io.BufferedReader;
import java.io.IOException;

public class ResponseHelper {
	
	private BufferedReader reader;

	public ResponseHelper(BufferedReader reader) {
		this.reader = reader;
	}
	
	public Response getResponse() 
	throws IOException {
		
		Response response = new Response();
		String line = reader.readLine();
		String code = line.substring(0, 3);
		response.setCode(Integer.valueOf(code));
		
		String msg = line.substring(4);
		response.setMessage(msg);
		
		String result = "";
		if (line.charAt(3) == '-') {	//multi-line
			line = reader.readLine();
			while (!line.startsWith(code + " ")) {
				result += line + "\n";
				line = reader.readLine();
			}
		}
		response.setResult(result);
		
		return response;
	}
	
	public void close() throws IOException {
		this.reader.close();
	}

}
